package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Userprojcet;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectData;
import com.ruoyi.system.service.IProjectDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.IUserprojcetService;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/system/data")
public class ProjectDataController extends BaseController
{
    @Autowired
    private IProjectDataService projectDataService;

    @Autowired
    private IUserprojcetService userprojcetService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectData projectData)
    {
        startPage();
        List<ProjectData> list = projectDataService.selectProjectDataList(projectData);
        return getDataTable(list);
    }

    /**
     * 公司用户查询项目注册列表
     */
//    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list/{id}")
    public TableDataInfo Comlist(ProjectData projectData,@PathVariable("id") Long id)
    {
        startPage();
        Userprojcet userprojcet=new Userprojcet();
        userprojcet.setComuser(id);
        List<Userprojcet> projectList = userprojcetService.selectUserprojcetList(userprojcet);
        List<ProjectData> list=projectDataService.selectProjectDataList(projectData);
        List<ProjectData> ansList=new ArrayList<>();
        for (ProjectData data : list) {
            for (Userprojcet projcet : projectList) {
                if(data.getId()==projcet.getProject()){
                    ansList.add(data);
                }
            }
        }
        return getDataTable(ansList);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectData projectData)
    {
        List<ProjectData> list = projectDataService.selectProjectDataList(projectData);
        ExcelUtil<ProjectData> util = new ExcelUtil<ProjectData>(ProjectData.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectDataService.selectProjectDataById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectData projectData)
    {
        AjaxResult result=toAjax(projectDataService.insertProjectData(projectData));
        return result.put("id",projectData.getaId());
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectData projectData)
    {
        return toAjax(projectDataService.updateProjectData(projectData));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectDataService.deleteProjectDataByIds(ids));
    }
}
